<?php

/**
 * ตารางใช้บอกว่า Supplier ติดต่อกับ Customer ใดได้บ้าง
 */
class CustomerSupplier extends AppModel {

    var $name = 'CustomerSupplier';
    var $useTable = 'customers_suppliers';
    var $belongsTo = array('Customer', 'Supplier');

    /**
     * ดึง list Customer ตาม Supplier ID ที่ระบุ
     * default condition ดึงเฉพาะลูกค้าที่มีคลังอยุ่ที่ Vendor
     * @param type $supplier_id
     * @param type $conditions
     * @return type 
     */
    function listCustomerBySupplierId($supplier_id, $conditions = null) {

        if ($conditions == null) {
            $conditions['has_supplier_inventory'] = true;
        }
        $conditions['supplier_id'] = $supplier_id;

        $customers = $this->find('list', array(
                    'fields' => array('id', 'customer_id'),
                    'conditions' => $conditions
                        )
        );
        
        return $this->Customer->find('list', array('conditions' => array('Customer.id' => array_values($customers))));
    }

    /**
     * ดึง list Supplier ตาม Customer ID ที่ระบุ
     * default condition ดึงเฉพาะที่มีคลังลูกค้าอยุ่ที่ Vendor
     * @param type $customer_id
     * @param type $conditions
     * @return type 
     */
    function listSupplierByCustomerId($customer_id, $conditions = null) {

        if ($conditions == null) {
            $conditions['has_supplier_inventory'] = true;
        }
        $conditions['customer_id'] = $customer_id;

        $suppliers = $this->find('list', array(
                    'fields' => array('id', 'supplier_id'),
                    'conditions' => $conditions
                        )
        );
        
        return $this->Supplier->find('list', array('conditions' => array('Supplier.id' => array_values($suppliers))));
    }

}

?>
